<?php

class BannerMultiuploadForm extends BaseBannerForm{

	public function configure(){
		unset($this['created_at'],$this['updated_at'],$this['pri']);
		
		// file data
      	$this->widgetSchema['file'] = new sfWidgetFormInputFile();
    	$this->validatorSchema['file'] = new sfValidatorFile(
    		array( 	'required' => false,
    			'max_size'=>'10240000', ) );

		
	}


  public function processForm($controller, $request)
  {
  	$bindArray = array(
		'banner_group_id' => $request->getParameter('banner_group_id'),
		'type' => 'default',
		'name' => 'no name'
	);

  	$files = $request->getFiles();

	$this->bind( $bindArray, array( 'file' => $files['Filedata'] ) );
  	$this->save();
  }

  protected function doSave($con = null)
  {

    $file = $this->getValue('file');
    if($file){
        //$filename = sha1($file->getOriginalName() ). $file->getExtension($file->getOriginalExtension());
        $filename = $file->getOriginalName();
        $file->save(sfConfig::get('sf_upload_dir').'/banner/'.$filename);
    }

    return parent::doSave($con);
  }


  public function updateObject($values = null)
  {
    $object = parent::updateObject($values);
    if( $object->getFile() ){
    	$object->setFile(str_replace(sfConfig::get('sf_upload_dir'), '', $object->getFile()));
    }


    if( !$object->file && $this->getValue('file') ){
    	$object->file = $this->getValue('file')->getOriginalName();
    }

    return $object;
  }


}
